package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.MedalDao;
import com.gdit.zax.entitys.CourseCollect;
import com.gdit.zax.entitys.Medal;
import com.gdit.zax.entitys.UserMedal;
import com.gdit.zax.utils.DBUtil;

/**
 * 勋章
 * @author lenovo
 *
 */
public class MedalDaoImpl implements MedalDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public MedalDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public boolean addUserMedal(int uid, int mid) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean delUserMedal(int uid, int mid) {
		// TODO Auto-generated method stub
		return false;
	}

	public int getUserMedalCount(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<UserMedal> listUserMedal(int uid, int page, int onepagecount) {
		String sql = "select * from t_usermedal as um INNER JOIN t_medal as m on um.MedalId=m.Id INNER JOIN t_user as u on um.UserId=u.Id where um.UserId=? limit ?,?";
		ArrayList<UserMedal> list = new ArrayList<UserMedal>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			while(rs.next()){
				UserMedal be = DBUtil.getJoinObject(rs, UserMedal.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}

	public ArrayList<Medal> listAllMedal(int page, int onepagecount) {
		String sql = "select * from t_medal limit ?,?";
		ArrayList<Medal> list = new ArrayList<Medal>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			
			rs = ps.executeQuery();
			while(rs.next()){
				Medal be = DBUtil.getobject(rs, Medal.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
